package com.big_data_studio.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 解决springboot和vue端口不一致，出现跨域的问题
 *
 * @author : 曾小杰
 * @date : 2022-03-28 22:44
 **/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Value("${reggie.path}")
    String folder;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")      //允许所有请求的路径
                .allowedOrigins("*")              // 允许所有请求的跨域源
                .allowCredentials(true)
                .allowedMethods(
                        "GET",
                        "POST",
                        "DELETE",
                        "PUT")
                .allowedHeaders("*")
                .maxAge(3600 * 24);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/file/**").addResourceLocations("file:" + folder + "/");
        //Handler 访问路径  Locations  映射为真实路径
    }
}
